package com.tbit.uqbike.webmanager.dto;

import cn.hutool.core.collection.CollectionUtil;
import com.tbit.uqbike.enums.CancelType;
import com.tbit.uqbike.enums.TagBindStatus;
import com.tbit.uqbike.enums.UserType;
import com.tbit.uqbike.object.business.UserInfo;
import com.tbit.uqbike.service.business.tag.UpdateBindStatusReq;
import com.tbit.uqbike.service.business.tag.validation.group.bind.*;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 标签绑定DTO
 *
 *
 * @author <a href="mailto:chen.chaoxiong@tbit.com.cn">ChenChaoXiong</a>
 * @since 2024/7/9
 */
@Data
public class TagBindDTO {

    @NotEmpty(groups = {OperationByTagBindIdGroup.class})
    private Set<Long> ids;

    /**
     * 品牌ID
     */
    @NotNull(groups = {CreateTagBindGroup.class})
    private Integer brandId;

    /**
     * 区域ID
     */
    @NotNull(groups = {CreateTagBindGroup.class})
    private Integer accountId;


    /**
     * 用户信息
     */
    @NotNull(message = "用户信息不能为空")
    private UserInfo userInfo;


    /**
     * 设备id
     */
    @NotEmpty(message = "设备id不能为空", groups = {OperationByMachineIdGroup.class})
    private Set<Integer> machineIds;


    /**
     * 设备编号
     */
    @NotEmpty(message = "设备编号不能为空", groups = {OperationByMachineNoGroup.class})
    private Set<String> machineNos;

    /**
     * 设备编码
     */
    @NotEmpty(message = "设备编码不能为空", groups = {OperationByUserCodeGroup.class})
    private Set<String> userCodes;


    @NotBlank(groups = {BatchOperationTestGroup.class})
    private String machineInfoType;


    /**
     * 标签ID
     */
    @NotEmpty(groups = {OperationByTagIdGroup.class,OperationByMachineNoGroup.class})
    private Set<Long> tagIds;


    /**
     * 用户类型
     * @see UserType
     */
    @NotNull(groups = {OperationByTagIdGroup.class})
    private UserType userType;

    @NotNull(groups = {CancelTagGroup.class})
    private CancelType cancelType;




    public  List<UpdateBindStatusReq> buildCancelReqFromMachineId() {
        Set<Integer> machineIds = getMachineIds();
        if (CollectionUtil.isEmpty(machineIds)) {
            return Collections.emptyList();
        }
        return machineIds.stream().map(machineId ->
                {
                    UpdateBindStatusReq req = new UpdateBindStatusReq();
                    req.setMachineId(machineId);
                    req.setBrandId(getBrandId());
                    req.setAccountId(getAccountId());
                    req.setCancelTagTime(LocalDateTime.now());
                    req.setCancelType(cancelType);
                    req.setUserType(userType);
                    req.setExpectBindStatus(TagBindStatus.BINDING);
                    req.setNewBindStatus(TagBindStatus.CANCELED);
                    req.setOperatorId(getUserInfo().getAccountUserId());
                    req.setTagIds(tagIds);
                    return req;
                }

        ).collect(Collectors.toList());
    }

    public List<UpdateBindStatusReq> buildCancelReqFromTagId() {
        Set<Long> tagIds = getTagIds();
        if (CollectionUtil.isEmpty(tagIds)) {
            return Collections.emptyList();
        }
        return tagIds.stream().map(tagId ->
                {
                    UpdateBindStatusReq req = new UpdateBindStatusReq();
                    req.setTagId(tagId);
//                    req.setBrandId(getBrandId());
//                    req.setAccountId(getAccountId());
                    req.setCancelTagTime(LocalDateTime.now());
                    req.setCancelType(cancelType);
                    req.setUserType(userType);
                    req.setExpectBindStatus(TagBindStatus.BINDING);
                    req.setNewBindStatus(TagBindStatus.CANCELED);
                    req.setOperatorId(getUserInfo().getAccountUserId());
                    return req;
                }

        ).collect(Collectors.toList());
    }

    public List<UpdateBindStatusReq> buildCancelReqFromId() {
        Set<Long> ids = getIds();
        if (CollectionUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return ids.stream().map(id ->
                {
                    UpdateBindStatusReq req = new UpdateBindStatusReq();
                    req.setId(id);
                    req.setBrandId(getBrandId());
                    req.setAccountId(getAccountId());
                    req.setCancelTagTime(LocalDateTime.now());
                    req.setCancelType(cancelType);
                    req.setUserType(userType);
                    req.setExpectBindStatus(TagBindStatus.BINDING);
                    req.setNewBindStatus(TagBindStatus.CANCELED);
                    req.setOperatorId(getUserInfo().getAccountUserId());
                    return req;
                }

        ).collect(Collectors.toList());
    }

}
